package com.cy.pet.cart.controller;

import com.cy.pet.common.pojo.Cart;
import com.cy.pet.common.service.CartService;
import com.cy.pet.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/10/8 10:40
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    /*根据用户名获取购物车列表*/
    @GetMapping("/getAll/{userName}")
    public SysResult cartGetAll(@PathVariable String userName){
        List<Cart> cartList = cartService.cartGetAll(userName);
        if (cartList.isEmpty()) return SysResult.fail();
        return SysResult.success(cartList);
    }
    /*从购物车中删除单个商品*/
    @DeleteMapping("/deleteOne")
    public SysResult cartDeleteOne(Integer id){
        System.out.println(id);
        Boolean flag = cartService.cartDeleteOne(id);
        if(flag) return SysResult.success();
        else return SysResult.fail();
    }
    /*从购物车中删除所有商品*/
    @DeleteMapping("/deleteAll")
    public SysResult cartDeleteAll(String userName){
        Boolean flag = cartService.cartDeleteAll(userName);
        if(flag) return SysResult.success();
        else return SysResult.fail();
    }
    /*http://localhost:8401/cart/addCart*/
    /*添加购物车*/
    @PostMapping("/addCart")
    public SysResult addCart(String userName,Integer itemId){
        Boolean flag = cartService.addCart(userName,itemId);
        if(flag) return SysResult.success();
        return SysResult.fail();
    }
}

